package org.iabako.server.controller.timer.logtrack;

import org.apache.log4j.Logger;
import org.iabako.server.conf.AppConf;
import org.iabako.server.mail.IabakoMailHelper;
import org.iabako.server.mail.IabakoMailSender;
import org.iabako.server.tools.ServerTools;
import org.iabako.shared.tools.GenericTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by jose on 14/05/15.
 */
@Controller
public class LogTrackControllerTimer {

    static Logger log = Logger.getLogger(LogTrackControllerTimer.class.getName());

    @Autowired
    private IabakoMailSender iabakoMailSender;
    @Autowired
    private AppConf appConf;
    @Autowired
    private ApacheLogTrackController apacheLogTrackController;
    @Autowired
    private ProspectsLogTrackController prospectsLogTrackController;
    @Autowired
    private ActivityLogTrackController activityLogTrackController;
    @Autowired
    private ErrorLogTrackController errorLogTrackController;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    //Every day at 02:00 AM
    @Scheduled(cron = "0 0 2 * * ?")
    //@Scheduled(initialDelay = 1000, fixedRate = 600000)
    public void action() throws Exception {
        log.info("Executing LogTrackControllerTimer...");

        ServerTools serverTools = new ServerTools();
        Date yesterday = serverTools.addDaysToDate(serverTools.resetTime(new Date()), -1);

        String subjectVisitors = "[LOG] Visitors tracking of " + GenericTools.formatDate(yesterday);
        String subjectProspects = "[LOG] Prospects tracking of " + GenericTools.formatDate(yesterday);
        String subjectActivities = "[LOG] Activities tracking of " + GenericTools.formatDate(yesterday);
        String subjectErrors = "[LOG] Errors tracking of " + GenericTools.formatDate(yesterday);

        sendLogTrackByEmail(subjectVisitors, apacheLogTrackController.trackApacheLogs());
        sendLogTrackByEmail(subjectProspects, prospectsLogTrackController.trackProspectsLogs());
        sendLogTrackByEmail(subjectActivities, activityLogTrackController.trackActivitiesLogs());
        sendLogTrackByEmail(subjectErrors, errorLogTrackController.trackErrorLogs());

        log.info("LogTrackControllerTimer ends successfully");
    }

    private void sendLogTrackByEmail(String subject, String bodyMessage) {
        if (GenericTools.isEmpty(bodyMessage)) {
            log.warn("There is no email body to send...");
            return;
        }
        iabakoMailSender.sendMail(new IabakoMailHelper(
                appConf.getNoReplyEmail(),
                appConf.getEmailPersonalFrom(),
                appConf.getClientServiceEmail(),
                subject,
                bodyMessage));
    }
}
